package cn.lijiajia3515.cairo.auth.domain.mongo;

import cn.lijiajia3515.cairo.mongo.data.AbstractMongo;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import lombok.experimental.SuperBuilder;
import org.bson.types.ObjectId;
import org.springframework.data.mongodb.core.mapping.MongoId;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;


/**
 * 账号 mongo结构
 */
@EqualsAndHashCode(callSuper = true)
@Data
@Accessors(chain = true)
@NoArgsConstructor
@AllArgsConstructor
@SuperBuilder(toBuilder = true)
public class AccountMongo extends AbstractMongo {

	/**
	 * 标识
	 */
	@MongoId
	private ObjectId _id;

	/**
	 * cairo id
	 */
	private String id;

	/**
	 * 名称
	 */
	private String name;

	/**
	 * 用户名
	 */
	private String username;

	/**
	 * 密码
	 */
	private String password;

	/**
	 * 邮箱号码
	 */
	private String email;

	/**
	 * 手机号码
	 */
	private String phoneNumber;

	/**
	 * 头像
	 */
	private String avatarUrl;

	/**
	 * 账号 启用
	 */
	@Builder.Default
	private Boolean accountEnabled = true;

	/**
	 * 账号 锁定
	 */
	@Builder.Default
	private Boolean accountLocked = false;

	/**
	 * 账号是否是管理员
	 */
	@Builder.Default
	private Map<String, Boolean> accountAdmin = Collections.emptyMap();

	/**
	 * 角色
	 */
	private Map<String, Set<String>> role;

	/**
	 * 部门
	 */
	private Map<String, Set<String>> department;

	/**
	 * 资源
	 */
	private Map<String, Set<String>> resource;

	/**
	 * 额外权限
	 */
	private Map<String, Set<String>> authority;

	/**
	 * 最后登录时间
	 */
	private LocalDateTime lastLoginAt;


	public static final Field FIELD = new Field();

	public static class Field extends AbstractMongo.Field {
		private Field() {

		}

		public final String ID = field("Id");
		public final String NAME = field("Name");
		public final String USERNAME = field("Username");
		public final String PASSWORD = field("Password");
		public final String EMAIL = field("Email");
		public final String PHONE_NUMBER = field("PhoneNumber");

		public final String AVATAR_URL = field("AvatarUrl");
		public final String LAST_LOGIN_AT = field("LastLoginAt");

		public final String ACCOUNT_ENABLED = field("AccountEnabled");
		public final String ACCOUNT_LOCKED = field("AccountLocked");
		public final Map ACCOUNT_ADMIN = new Map(this, "Admin");

		public final Map ROLE = new Map(this, "Role");
		public final Map DEPARTMENT = new Map(this, "Department");
		public final Map RESOURCE = new Map(this, "Resource");
		public final Map AUTHORITY = new Map(this, "Authority");

		public static abstract class AbstractClientField extends AbstractMongo.Field {
			public AbstractClientField() {

			}

			public AbstractClientField(AbstractMongo.Field parent, String prefix) {
				super(parent, prefix);
			}

			public String client(String client) {
				return field(client);
			}
		}

		public static class Map extends AbstractMongo.Field {

			public Map(AbstractMongo.Field parent, String prefix) {
				super(parent, prefix);
			}

			public String client(String client) {
				return field(client);
			}
		}
	}
}
